package agraph;

import com.franz.agbase.*;

public class AGraph {

	
	AllegroGraph ts;
	AllegroGraphConnection ags;
	
	public static void main(String[] args) throws AllegroGraphException {
		
	AGraph AGraph = new AGraph();
	UserHandling UH = new UserHandling();
	
	// Connect to the default server, which must already be running.
	AGraph.ags = new AllegroGraphConnection();
	try {
		// This example connects to a server on the default port.
		// For servers listening on a non-default port, e.g. 4126,
		// uncomment this to specify the port:
		//ags.setPort(4126);
		System.out.println("Attempting to connect to the server on port " + AGraph.ags.getPort());
		AGraph.ags.enable();
	} catch (Exception e) {
		throw new AllegroGraphException("Server connection problem -- please ensure the server is running.", e);
	}

	System.out.println("Connection enabled.");
	
	
	// Create a fresh triple store.
	AGraph.ts = AGraph.ags.renew("loadRDF", AGPaths.TRIPLE_STORES);

	// Load a server-side file in RDF/XML format into the store's default graph.
	//loadRDFWithTiming(ts, AGPaths.dataSources("rdf-axioms.rdf"));
	loadRDFWithTiming(AGraph.ts, AGPaths.dataSources("sigc.owl"));
	// Get some triples from the default graph and show them.
	TriplesIterator it = AGraph.ts.getStatements(null, null, null);
	//AGUtils.showTriples(it, 200);
	
	//AGUtils.printStringArray("Namespace Registry:", AGraph.ts.getNamespaces());
	
	/*///teste registo user...
	UH.userRegister(AGraph, "email","login","password","primeiro_nome","ultimo_nome");
	
	//teste login
	UH.userLogin(AGraph, "monteiro", "xpto");
	
	//teste subscribe
	UH.userSubscribe(AGraph, "monteiro", "apple");
	
	UH.userUnsubscribe(AGraph, "monteiro", "apple");*/
	
	// Disconnect from the server
	System.out.println("Disconnecting from the server.");
	AGraph.ags.disable();
	System.out.println("Done.");
	
	}
	
	/**
	 * Load a single RDF/XML file into the default graph and time the load.
	 * 
	 * @param ts A triple store 
	 * @param rdfFile A server-accessible RDF/XML file
	 * @throws AllegroGraphException
	 */
	public static void loadRDFWithTiming(AllegroGraph ts, String rdfFile) throws AllegroGraphException {
		loadRDFWithTiming(ts, rdfFile, "");
	}
	
	/**
	 * Load a single RDF/XML file into the specified graph and time the load.
	 * 
	 * @param ts A triple store 
	 * @param rdfFile A server-accessible RDF/XML file
	 * @param graph The context to load
	 * @throws AllegroGraphException
	 */
	public static void loadRDFWithTiming(AllegroGraph ts, String rdfFile, Object graph) throws AllegroGraphException {
		System.out.println("Loading RDF from " + rdfFile);
		long start = System.currentTimeMillis();
		long n = ts.loadRDFXML(rdfFile, graph);
		System.out.println("Done loading " + n + " triples in " + AGUtils.elapsedTime(start));
	}
}